package com.fhz.day003;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    long time =  0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    /**
     * 该方法用来捕捉手机键盘被按下的事件。
     * @param keyCode  该参数指的是被按下的键的键盘码，手机键盘中每个按钮都会有其对应的键盘码，
     *                 在应用程序都是通过键盘码才知道用户按下的是哪个键。
     * @param event    当用户按下按键时，系统会自动将事件封装成KeyEvent对象供应用程序使用。
     *                 按键事件KeyEvent 按键事件有多种常量类型，比如 KEYCODE_BACK
     * @return  是否消费当前的时间
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            //从1970 0,0 到现在所有时间的毫秒值
            //10000-9999 > 2000
            if (System.currentTimeMillis() - time > 2000){
                Toast.makeText(MainActivity.this, "在按就推出", Toast.LENGTH_SHORT).show();
                time = System.currentTimeMillis();
                return true;
            }else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void notify1(View view) {
        Notification.Builder builder = new Notification.Builder(this);

        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentText("我是内容");
        builder.setContentTitle("我是标题");
        builder.setAutoCancel(true);

        Notification build = builder.build();

        //管理通知
        NotificationManager systemService = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        systemService.notify(1,build);

    }

    public void notify2(View view) {
        Notification.Builder builder = new Notification.Builder(this);

        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentText("我是内容");
        builder.setContentTitle("我是标题");
        builder.setAutoCancel(true);

        //getPackageName()
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);

        remoteViews.setTextViewText(R.id.notify_tv,"真帅");
        //对图片的大小有要求.
        remoteViews.setImageViewResource(R.id.notify_img,R.mipmap.ic_launcher_round);


        Intent intent = new Intent(this, MainActivity2.class);
        //延迟的intent
        PendingIntent activity = PendingIntent.getActivity(this, 888, intent, 0);
        remoteViews.setOnClickPendingIntent(R.id.notify_img,activity);


        builder.setCustomContentView(remoteViews);

        Notification build = builder.build();
        //管理通知
        NotificationManager systemService = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //相同会覆盖
        systemService.notify(2,build);
    }

    public void notify3(View view) {

        //管理通知
        NotificationManager systemService = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(this);

        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("下载");
        builder.setAutoCancel(true);


        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int progress = 0;
            @Override
            public void run() {
                builder.setContentText("正在下载....");
                builder.setProgress(100,progress,false);
                progress += 10;
                systemService.notify(3,builder.build());
                if(progress >= 100){
                    builder.setContentText("正在安装....");
                    builder.setProgress(0,0,true);
                    systemService.notify(3,builder.build());

                    try {
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //取消定时器和通知
                    systemService.cancel(3);
                    timer.cancel();
                }
            }
        },0,1000);
        systemService.notify(3,builder.build());
    }

    public void notify4(View view) {
        //管理通知
        NotificationManager systemService = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder mBuilder0 = new Notification.Builder(this);
        Notification.Builder mBuilder1 = new Notification.Builder(this);
        Notification.Builder mBuilder2 = new Notification.Builder(this);
        Notification.Builder mBuilder3 = new Notification.Builder(this);
        //消息分组属性,group相同才能分到一组
        mBuilder0.setGroup("2");
        mBuilder0.setSmallIcon(R.mipmap.ic_launcher);
        //被设置setGroupSummary为true的消息会隐藏内容,其内容会显示另外分组成员信息.
//        mBuilder0.setGroupSummary(true);
        mBuilder0.setContentText("11111111");
        mBuilder0.setContentTitle("222222222");

//        mBuilder0.setVisibility(Vis)


        mBuilder1.setGroup("2");
        mBuilder1.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder1.setGroupSummary(true);
        mBuilder1.setContentText("333333");
        mBuilder1.setContentTitle("44444444");


        mBuilder2.setGroup("2");
        mBuilder2.setSmallIcon(R.mipmap.ic_launcher);
//        mBuilder2.setGroupSummary(true);
        mBuilder2.setContentText("55555555555555555");
        mBuilder2.setContentTitle("66666666666");

        mBuilder3.setGroup("2");
        mBuilder3.setSmallIcon(R.mipmap.ic_launcher);
//        mBuilder3.setGroupSummary(true);
        mBuilder3.setContentText("77777");
        mBuilder3.setContentTitle("8888888");


        systemService.notify(0,mBuilder0.build());
        systemService.notify(1,mBuilder1.build());
        systemService.notify(2,mBuilder2.build());
        systemService.notify(3,mBuilder3.build());

    }

    public void notify5(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("大图通知");

        //大图通知
//        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
//        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));

        //列表通知
        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
        inboxStyle.addLine("第一行");
        inboxStyle.addLine("第二行");
        inboxStyle.addLine("第三行");

        builder.setStyle(inboxStyle);

        manager.notify(5,builder.build());


    }

    public void notify6(View view) {

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("可点击的通知");

        Intent intent = new Intent(this, MainActivity2.class);
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        //阅读以后自动消息.
        builder.setAutoCancel(true);

        //可以弹出来的通知
        builder.setPriority(Notification.PRIORITY_MAX);
        builder.setDefaults(Notification.DEFAULT_ALL);

        builder.setContentIntent(activity);

        manager.notify(6,builder.build());

    }
}